package com.yudean.esb.dao.esb;

import com.yudean.esb.domain.esb.EsbRecvServiceTransaction;
import com.yudean.esb.mapper.esb.EsbRecvServiceTransactionMapper;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class EsbRecvServiceTransactionDao {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(EsbRecvServiceTransactionDao.class);
    @Resource
    private EsbRecvServiceTransactionMapper esbRecvServiceTransactionMapper;

    public List<EsbRecvServiceTransaction> select(String serviceID) {
        if (serviceID == null || serviceID.length() == 0) {
            return null;
        }
        return esbRecvServiceTransactionMapper.select(serviceID);
    }

    public void delete(EsbRecvServiceTransaction transaction) {
        esbRecvServiceTransactionMapper.delete(transaction);
    }


    public boolean insert(EsbRecvServiceTransaction transaction) {
        boolean result = false;
        try {
            esbRecvServiceTransactionMapper.insert(transaction);
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    public void update(EsbRecvServiceTransaction transaction) {
        esbRecvServiceTransactionMapper.update(transaction);
    }

    public boolean insert(String serviceID, String transactionID,
                          String totalPackage, String rollback) {
        logger.info(
                "insert():request-{serviceID:{},transactionID:{},totalPackage:{},rollback:{}}",
                serviceID, transactionID, totalPackage, rollback);
        EsbRecvServiceTransaction transaction = new EsbRecvServiceTransaction();
        transaction.setTransactionID(transactionID);
        transaction.setServiceID(serviceID);
        transaction.setTotalPackage(totalPackage);
        transaction.setRollback(rollback);
        return insert(transaction);
    }

    /**
     * 根据服务ID获得所有的与服务关联的事务
     *
     * @param serviceID
     * @return
     */
    public List<EsbRecvServiceTransaction> selectByServiceId(String serviceID, String operation) {
        if (serviceID == null || "".equals(serviceID.trim())) return null;
        operation = (operation == null || "".equals(operation.trim())) ? "I" : operation;//设置默认为I

        List<EsbRecvServiceTransaction> result = esbRecvServiceTransactionMapper.selectByServiceId(serviceID.trim(), operation);
        if (result == null || result.size() == 0) return null;
        return result;
    }

    /**
     * 获得所有待转移到目标表的事务列表
     *
     * @param serviceID
     * @return
     */
    public List<EsbRecvServiceTransaction> selectNoneHandled(String serviceID) {
        if (serviceID == null || "".equals(serviceID.trim())) return null;
        List<EsbRecvServiceTransaction> result = esbRecvServiceTransactionMapper.selectNoneHandled(serviceID.trim());
        if (result == null || result.size() == 0) return null;
        return result;
    }

    /**
     * 判断一个事务下的包是否已经完全写入到库中了，
     * 避免
     *
     * @param transactionID
     * @return
     */
    public boolean isFullPackage(String transactionID) {
        boolean is = false;
        if (transactionID == null || "".equals(transactionID)) {
            return true;
        }
        int count = esbRecvServiceTransactionMapper.isFullPackage(transactionID);
        return count > 0;
    }
}
